<?php

namespace App\Console\Commands\Transfer;

use App\AppContext\Console;
use App\Facade\AppContext;
use App\Repositories\Ca\ConsultationWayRepository;
use App\Repositories\Ca\DiseaseRepository;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class DiseaseCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'transfer:disease';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '迁移';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $data = DB::connection('S')->table('bs_disease')->orderBy('pid')->get();
        app()->singleton("AppContext", Console::class);
        foreach ($data as $datum) {
            AppContext::hospitalId($datum->hospital_id);
            $rep = new DiseaseRepository();
            $result = $rep->save([
                'id' => $datum->id,
                'name' => $datum->name,
                'pid' => $datum->pid,
                'weight' => $datum->weight,
                'deleted_at' => $datum->status == 1 ? null : date('Y-m-d H:i:s'),
                'hospital_id' => $datum->hospital_id < 0 ? 0 : $datum->hospital_id
            ]);
            if ($result->isError()) {
                $this->output->warning($result->msg);
            }
        }
    }
}